package com.woniuxy.controller;


import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.User;
import com.woniuxy.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
@RequiresRoles(value = {"superAdmin","admin"},logical = Logical.OR)
public class UserController {
    @Autowired(required = false)
    UserService userService;

    @GetMapping("/{currentPage}/{pageSize}")
    @ApiOperation(value = "获取分页用户数据")
    public ResponseEntity findAll(@ApiParam(name = "currentPage", value="当前页码",required=true)@PathVariable int currentPage,
                                  @ApiParam(name = "pageSize", value="页面大小",required=true)@PathVariable int pageSize){
        return userService.findAll(currentPage, pageSize);
    }

    @RequiresPermissions("user:add")
    @PostMapping("/add")
    @ApiOperation(value = "新增用户")
    public ResponseEntity addOne(@ApiParam(name = "user", value="用户实体",required=true) @RequestBody User user){
        return userService.addOne(user);
    }

    @RequiresPermissions("user:update")
    @PutMapping("/update")
    @ApiOperation(value = "修改用户")
    public ResponseEntity updateOne(@ApiParam(name = "user", value="用户实体",required=true) @RequestBody User user){
        return userService.updateOne(user);
    }

    @RequiresPermissions("user:delete")
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除用户")
    public ResponseEntity deleteOne(@ApiParam(name = "id", value="用户id",required=true) @PathVariable int id){
        return userService.deleteOne(id);
    }

    @PostMapping("/serch")
    @ApiOperation(value = "查询用户")
    public ResponseEntity findByContext(@ApiParam(name = "user", value="用户实体",required=true) @RequestBody User user){
        return userService.findByContext(user);
    }

    @GetMapping("/{username}")
    @ApiOperation(value = "根据用户名查询用户")
    public User findByUsername(@ApiParam(name = "username", value="用户名",required=true) @PathVariable String username){
        return userService.findByUsername(username);
    }

    @PutMapping("/password")
    @ApiOperation(value = "修改密码")
    public ResponseEntity updatePassword(@ApiParam(name = "user", value="用户实体",required=true) @RequestBody Object obj){
        return userService.updatePassword(obj);
    }

    @RequiresPermissions("user:batchDelete")
    @PutMapping("/batchDelete")
    @ApiOperation(value = "批量删除用户")
    public ResponseEntity batchDelete(@ApiParam(name = "ids", value="用户id数组",required=true)@RequestBody int[] ids){
        return userService.batchDelete(ids);
    }

    @GetMapping("/getPassword/{id}")
    @ApiOperation(value = "获取密码")
    public ResponseEntity getPassword(@ApiParam(name = "id", value="用户序号",required=true)@PathVariable int id){
        return userService.getPassword(id);
    }

    @GetMapping("/no/{no}")
    @ApiOperation(value = "根据工号查询用户工号")
    public ResponseEntity findByNo(@ApiParam(name = "no", value="组织编码") @PathVariable String no){
        return userService.findByNo(no);
    }

    @GetMapping("/username/{username}")
    @ApiOperation(value = "根据用户名查询用户名")
    public ResponseEntity findByName(@ApiParam(name = "username", value="用户名") @PathVariable String username){
        return userService.findByName(username);
    }
}
